package com.yjd.back.settlement;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.fundservice.enums.FundChannelEnum;
import com.yjd.comm.fundservice.model.FundLoanApplyModel;
import com.yjd.comm.fundservice.service.IFundLoanApplyService;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.settlement.model.ClearDayFundChannelModel;
import com.yjd.comm.settlement.service.IClearDayFundChannelService;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yjd-wap on 2018/3/13.
 */
@Scope("prototype")
@Controller
@RequestMapping("/fundChannel/settlement/")
public class ClearDayFundChannelControl extends BaseControl {

    @Resource
    private IClearDayFundChannelService clearDayFundChannelService;

    @Resource
    private IFundLoanApplyService fundLoanApplyService;

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private IUserDataService userDataService;
    /**
     * 渠道日期列表
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "list")
    public String list(String startDate, String endDate) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "clear_date desc");

        if(StringUtil.isNotEmpty(startDate)){
            paramMap.put("clear_date_gte", Integer.valueOf(startDate.replace("-", "")));
        }
        if(StringUtil.isNotEmpty(endDate)){
            paramMap.put("clear_date_lte", Integer.valueOf(endDate.replace("-", "")));
        }

        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.clearDayFundChannelService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/fundChannel/settlement/list"));
        for (ClearDayFundChannelModel model : (List<ClearDayFundChannelModel>)pager.getPagerModel().getDataList()) {
           FundChannelEnum channelEnum = FundChannelEnum.parse(model.getChannel_id().intValue());
            model.getStrMap().put("channel_name",channelEnum==null?"":channelEnum.getDesc());
            model.getStrMap().put("create_time", DateUtil.timestampToDateString(model.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }

        request.setAttribute("maps", paramMap);
        request.setAttribute("pager", pager);
        request.setAttribute("fundChannelList", FundChannelEnum.getFundChannelList());
        return "fundChannel.day.list";
    }

    @RequestMapping(value = "detail")
    public String detail(Long id) throws Exception {
       if(id==null){
           FrameUtil.throwBaseException1("请求参数不能为空");
       }

       ClearDayFundChannelModel model = this.clearDayFundChannelService.getModelById(id,DbWREnums.READ);
        if(model==null){
            FrameUtil.throwBaseException1("请求的记录不存在");
        }
        Long start_time =  DateUtil.getParseDateStartTime(model.getClear_date()+"");
        Long end_time = start_time + 86399;
        Map paramMap = FrameUtil.newHashMap("fund_channel",model.getChannel_id(),"start_time",start_time,"end_time",end_time);

        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.fundLoanApplyService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/fundChannel/settlement/detail"));
        List<FundLoanApplyModel> fundLoanApplyModels = (List<FundLoanApplyModel>)pager.getPagerModel().getDataList();
        Map<Long, String> orgIdAndName = organizationService.allOrgAndName();
        List<Long> userIdList = FrameUtil.getBeanFieldValues(fundLoanApplyModels, "user_id");
        Map<Long,UserDataModel> userMap = userDataService.getModelMapByIds(userIdList,DbWREnums.READ);

        for (FundLoanApplyModel applyModel : fundLoanApplyModels) {
            applyModel.getStrMap().put("channel_name",FundChannelEnum.parse(applyModel.getFund_channel().intValue()).getDesc());
            applyModel.getStrMap().put("create_time", DateUtil.timestampToDateString(applyModel.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            applyModel.getStrMap().put("org_name",orgIdAndName.get(applyModel.getId()));
            if(userMap!=null){
                UserDataModel userDataModel = userMap.get(applyModel.getUser_id());
                if(userDataModel!=null){
                    applyModel.getStrMap().put("user_name",userDataModel.getId_name());
                }
            }
        }

        request.setAttribute("pager", pager);
        return "fundChannel.day.detail";
    }

    /**
     * 生成结算
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "generate")
    public String generateSettlement(String startDate, String endDate) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);

        Long channel_id = null;
        if(StringUtil.isNotEmpty(paramMap.get("channel_id"))){
            channel_id = Long.valueOf(paramMap.get("channel_id").toString());
        }

        Long startTime = FrameUtil.getTime();
        if(StringUtil.isNotEmpty(startDate)){
            startTime = DateUtil.dateStringToTimestamp(startDate, DateUtil.PATTERN_yyyy_MM_dd);
        }

        Long endTime = FrameUtil.getTime();
        if(StringUtil.isNotEmpty(endDate)){
            endTime = DateUtil.dateStringToTimestamp(endDate, DateUtil.PATTERN_yyyy_MM_dd);
        }

        if(startTime > endTime){
            FrameUtil.throwBaseException1("日期不合法");
        }
        if(channel_id==null){
            clearDayFundChannelService.settlementAll(startTime, endTime);
        }else {
            clearDayFundChannelService.settlementAll(channel_id, startTime, endTime);
        }
        return "redirect:/fundChannel/settlement/list.action";
    }
}
